import router from '@/router'
const axios = require('axios');
// axios.defaults.headers.post['Content-Type'] = 'application/json';

const state = {
  sellerId: '',
  sellerAccount: '',
  sellerAddress: '',
  sellerPhone: '',
  sellerName: '',
  sellerLogo: '',
  sellerStates: '',
  token: ''
}
const mutations = {
  SET_SELLER_ID: (state, sellerId) => {
    state.sellerId = sellerId
  },
  SET_SELLER_ACCOUNT: (state, sellerAccount) => {
    state.sellerAccount = sellerAccount
  },
  SET_SELLER_ADDRESS: (state, sellerAddress) => {
    state.sellerAddress = sellerAddress
  },
  SET_SELLER_PHONE: (state, sellerPhone) => {
    state.sellerPhone = sellerPhone
  },
  SET_SELLER_SHOPNAME: (state, sellerName) => {
    state.sellerName = sellerName
  },
  SET_SELLER_LOGO: (state, sellerLogo) => {
    state.sellerLogo = sellerLogo
  },
  SET_SELLER_STATES: (state, sellerStates) => {
    state.sellerStates = sellerStates
  },
}
const actions = {
  update({
    commit
  }, seller) {
    return new Promise((resolve, reject) => {

      let data = new FormData()
      for (let item in seller) {
        data.append(item, seller[item])
      }
      axios.post("http://localhost:8080/seller/update", data).then(res => {
        if (res.data === '') {
          resolve(null)
        } else {
          commit('SET_SELLER_ID', res.data.sellerId)
          commit('SET_SELLER_ACCOUNT', res.data.sellerAccount)
          commit('SET_SELLER_ADDRESS', res.data.sellerAddress)
          commit('SET_SELLER_PHONE', res.data.sellerPhone)
          commit('SET_SELLER_SHOPNAME', res.data.sellerName)
          commit('SET_SELLER_LOGO', res.data.sellerLogo)
          commit('SET_SELLER_STATES', res.data.sellerStates)
          resolve(true)
        }
      }).catch(err => {
        reject(false)
      })
    })
  },
  login({
    commit
  }, seller) {
    return new Promise((resolve) => {
      axios.get("http://localhost:8080/seller/login", {
        params: {
          sellerAccount: seller.username,
          password: seller.password
        }
      }).then(response => {
        if (response.data !== null && response.data !== '') {
          commit('SET_SELLER_ID', response.data.sellerId)
          commit('SET_SELLER_ACCOUNT', response.data.sellerAccount)
          commit('SET_SELLER_ADDRESS', response.data.sellerAddress)
          commit('SET_SELLER_PHONE', response.data.sellerPhone)
          commit('SET_SELLER_SHOPNAME', response.data.sellerName)
          commit('SET_SELLER_LOGO', response.data.sellerLogo)
          commit('SET_SELLER_STATES', response.data.sellerStates)
          resolve(true)
        } else {
          resolve(false)
        }
      }).catch(err => console.log(err))
    })
  },
  logOut({
    commit
  }) {
    window.localStorage.removeItem('autoLogin')
    commit('SET_SELLER_ID', '')
    commit('SET_SELLER_ACCOUNT', '')
    commit('SET_SELLER_ADDRESS', '')
    commit('SET_SELLER_PHONE', '')
    commit('SET_SELLER_SHOPNAME', '')
    commit('SET_SELLER_LOGO', '')
    commit('SET_SELLER_STATES', '')
    router.push({
      path: '/'
    })
  }

}
export default {
  state,
  mutations,
  actions
}
